package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Pay;
import com.example.springboot.entity.User;
import com.example.springboot.service.PayService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者：刘涵茹
 * 日期：2024/4/1 11:09
 */

@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    PayService payService;

    @Resource
    UserService userService;


    /**
     * 删除信息
     */
    @HoneyLogs(operation = "支付记录", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        payService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除信息
     */
    @HoneyLogs(operation = "支付记录", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        payService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 多条件模糊查询信息
     * pageNum 当前的页码 (还未更改)
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name
                             ) {
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<Pay>().orderByDesc("id");  // 默认倒序，让最新的数据在最上面
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        Page<Pay> page = payService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Pay> records = page.getRecords();
        for (Pay recode : records){
            User user = userService.getById(recode.getBuyerId());
            if (user != null){
                recode.setBuyer(user.getUsername());
            }

        }
        return Result.success(page);
    }

    /**
     * 分页多个条件模糊查询
     * 仅查询自己角色的支付信息
     */
    @GetMapping("/selectByUserPage")
    public Result selectByUserPage(
            @RequestParam Integer pageNum,//页数或者页码
            @RequestParam Integer pageSize,//当前页总共有多少条数据 这两个都是前端传参过来的
            @RequestParam String name
    ) {//得到数组，并传到service中
        User currentUser = TokenUtils.getCurrentUser(); //获取当前登录的用户信息
        Integer buyerId = currentUser.getId();
        QueryWrapper<Pay> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        queryWrapper.like( "buyer_id", buyerId);
        Page<Pay> page = payService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Pay> records = page.getRecords();
        for (Pay recode : records){
            User user = userService.getById(buyerId);
            if (user != null){
                recode.setBuyer(user.getUsername());
            }
        }
        return Result.success(page);
    }
}
